<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" style="height: calc(100% - 2px);">
    <fieldset class="table-search-fieldset">
        <div style="margin: 10px 10px 10px 10px">
            <form class="layui-form layui-form-pane" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="funcName" placeholder="功能名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="token" placeholder="token" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                    </div>
                </div>
            </form>
        </div>
    </fieldset>

    <div class="layui-row">
        <div class="layui-col-md3">
<!--            <input type="text" name="appName" placeholder="应用名称" autocomplete="off" class="layui-input" style="margin: 10px 2px -8px 0px;" lay-search>-->
            <table id="app_table" lay-filter="app_table"></table>
        </div>
        <div class="layui-col-md9">
            <div class="layuimini-main">
                <script type="text/html" id="toolbar">
                    <div class="layui-btn-group">
                        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="add">
                            <i class="layui-icon">&#xe654;</i>
                        </button>
                        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="delete">
                            <i class="layui-icon">&#xe640;</i>
                        </button>
                    </div>
                </script>

                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                <script type="text/html" id="currentTableBar">
                    <a class="layui-btn layui-btn-xs layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
                </script>

            </div>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        const host = `${window.location.protocol}//${window.location.host}`;
        $.ajaxSetup({
            headers: {'Access-Token': sessionStorage.getItem('token'), 'userId': sessionStorage.getItem("userId")}
        });

        // 加载app应用列表
        table.render({
            elem: '#app_table',
            method: 'get',
            url: `${host}/app/page`,
            cols: [[
                {type:'radio'},
                {field: 'appName',title: '应用名称'}
            ]],
            even: true,
            page: false,
            limit: Number.MAX_VALUE,
            skin: 'line',
            parseData: (rest) => {
                return {
                    "code": rest.code,
                    "msg" : rest.msg,
                    "count" : rest.data.total,
                    "data" : rest.data.records
                }
            }
        });

        var checkAppId = -1, checkAppName = '';

        table.on('row(app_table)', (obj) => {
            table.checkStatus(obj.data.id)
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            obj.tr.find('i[class="layui-anim layui-icon"]').trigger('click');
        });

        loadFuncTable(checkAppId);

        table.on('radio(app_table)', (obj) => {
            checkAppId = obj.data.id;
            checkAppName = obj.data.appName;
            loadFuncTable(checkAppId);
        });

        function loadFuncTable(appId) {
            table.render({
                title: '功能列表',
                elem: '#currentTableId',
                method: 'get',
                url: `${host}/func/page`,
                toolbar: '#toolbar',
                defaultToolbar: ['filter', 'exports', 'print'],
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 80, title: '序号'},
                    {field: 'funcName', width: 150, title: '功能名称'},
                    {field: 'token', width: 200, title: 'TOKEN_KEY'},
                    {field: 'remark', width: 180, title: '说明'},
                    {title: '操作',fixed: 'right', minWidth: 120, toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line',
                parseData: (rest) => {
                    return {
                        "code": rest.code,
                        "msg" : rest.msg,
                        "count" : rest.data.total,
                        "data" : rest.data.records
                    }
                },
                request: {
                    pageName: 'current',
                    limitName: 'size'
                },
                where: { 'searchParams' : JSON.stringify({'appId': appId}) }
            });
        }

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            data.field.appId = checkAppId;
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: { current: 1 },
                where: { 'searchParams' : result }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                if (checkAppId === -1) {
                    layer.msg("请选择应用")
                    return;
                }

                var index = layer.open({
                    title: '新增功能',
                    btn: ['保存','保存并继续','关闭'],
                    type: 2,
                    shade: 0.2,
                    maxmin:false,
                    shadeClose: false,
                    area: ['60%', '50%'],
                    content: `add.html?timestamp=${new Date().getTime()}`,
                    success: (rowObj) => {
                        var body = layer.getChildFrame('body', index);
                        body.find('input[name="appId"]').val(checkAppId);
                        body.find('input[name="appName"]').val(checkAppName);
                    },
                    yes : (index, layero) => {
                        let addData = $(layero).find('iframe')[0].contentWindow.callbackData();
                        $.post(`${host}/func/save`, addData, (rest) => {
                            if (rest && "00" === rest.code) {
                                layer.msg(rest.msg);
                                $(".layui-laypage-btn")[0].click();
                                layer.close(index);
                            } else {
                                layer.msg(rest.msg);
                            }
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改应用',
                    btn: ['确定','关闭'],
                    type: 2,
                    shade: 0.2,
                    maxmin:false,
                    shadeClose: false,
                    area: ['60%', '50%'],
                    content: `edit.html?timestamp=${new Date().getTime()}`,
                    success: (layero, index) => {
                        var body = layer.getChildFrame('body', index);
                        body.find('input[name="id"]').val(obj.data.id);
                        body.find('input[name="appId"]').val(checkAppId);
                        body.find('input[name="appName"]').val(checkAppName);
                        body.find('input[name="token"]').val(obj.data.token);
                        body.find('input[name="funcName"]').val(obj.data.funcName);
                        body.find('textarea[name="remark"]').val(obj.data.remark);
                    },
                    yes : (index, layero) => {
                        let editData = $(layero).find('iframe')[0].contentWindow.callbackData();
                        $.post("/func/save", editData, (rest) => {
                            if (rest && "00" === rest.code) {
                                layer.msg(rest.msg);
                                $(".layui-laypage-btn")[0].click();
                                layer.close(index);
                            } else {
                                layer.msg(rest.msg);
                            }
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('是否确定删除', function (index) {
                    let id = obj.data.id;
                    $.post("/func/delete", {'id': id}, (rest) => {
                        layer.msg(rest.msg);
                        if (rest && "00" === rest.code) {
                            obj.del();
                        }
                    })
                    layer.close(index);
                });
            }
        });
    });
</script>
</body>

<style>
    html {
        height: calc(100% - 15px);
    }
    body {
        height: 100%;
    }
</style>
</html>